package KK.Grid 
{ 
	
	import KK.EMVC.View.View;
	
	import flash.display.Graphics;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class GridCalculate extends View
	{
		
		private var _tileW:Number;//高度格子像素
		private var _tileH:Number;//高度格子像素
		private var _xCount:int;//x向
		private var _yCount:int;//y
		
		public function GridCalculate()
		{
		}
		
		/**
		 * 设置大小  
		 * @param tileW
		 * @param tileH
		 * @param vCount
		 * @param uCount
		 * 
		 */
		public function SetGrid(tileW:int,tileH:int,vCount:int,uCount:int):void{
			
			this._tileH=tileH;
			this._tileW=tileW;
			this._xCount=vCount;
			this._yCount=uCount;
			
		}
		/**
		 * 画菱形边框网格(整体也为菱形)
		 * @param uCount  U轴向网格个数
		 * @param vCount  V轴向网格个数
		 * @param _tileW  网格宽
		 * @param _tileH  网格高
		 * @param _lineColor 网格颜色
		 * 
		 */
		public function drawDiamond(graphics:Graphics,_xCount:int, _yCount:int,_tileW:Number,_tileH:Number,_lineColor:uint = 0xff1212):void
		{
			
			SetGrid(_tileW,_tileH,_xCount,_yCount);
			
			graphics.lineStyle(1, _lineColor, 1);
			//U向坐标
			var u:int;
			//V向坐标
			var v:int;
			//线起始点像素坐标
			var originPos:Point;
			//线途经点像素坐标
			var lineToPos:Point;
			
			//沿V向画线
			while(u <= _xCount)
			{
				v = 0;
				originPos = getPixelPoint(u,v);
				graphics.moveTo(originPos.x,originPos.y);
				
				while(v < _yCount)
				{
					++v;
					lineToPos = getPixelPoint(u,v);
					graphics.lineTo(lineToPos.x,lineToPos.y);
				}
				++u;
			}
			
			u = 0;
			v = 0;
			
			//沿U向画线
			while(v <= _yCount)
			{
				u = 0;
				originPos = getPixelPoint(u,v);
				graphics.moveTo(originPos.x,originPos.y);
				
				while(u < _xCount)
				{
					++u;
					lineToPos = getPixelPoint(u,v);
					graphics.lineTo(lineToPos.x,lineToPos.y);
				}
				++v;
			}
		}
		
		/**
		 * 获取UV点坐标
		 * @param _x x坐标
		 * @param _y y坐标
		 * @param _tw 网格宽
		 * @param _th 网格高
		 * @param _count U向网格数量
		 * @return 
		 * 
		 */
		public function TouchGrid(_x:Number,_y:Number):Point
		{
			//LogInfo("TouchGrid:x y",_x,_y);
			var org_x:Number = 0;
			var org_y:Number = this._yCount * this._tileH/2;
			 							//计算本身x处于第几个 由于如果y增加就会对应的退后所以要减去y的高度
			var _v:int = Math.floor((_x - org_x)/this._tileW- (_y - org_y)/this._tileH);
									//计算x的位置处于第几个由于x的位置增加y的位置也会对应增加
			var _u:int = Math.floor((_x - org_x)/this._tileW + (_y - org_y)/this._tileH);
			 
			
			
			return new Point(_u,_v);
		}
		
		/**
		 * 获取像素点坐标
		 * @param _u U向坐标
		 * @param _v v向坐标
		 * @param _tw 网格宽
		 * @param _th 网格高
		 * @param _count 网格数量
		 * @return 像素点x,y坐标
		 */
		public function getPixelPoint(x:int,y:int):Point
		{
			var org_x:Number = 0;
			var org_y:Number = this._yCount * this._tileH/2;
								//每格宽度乘于数量等于格子的x坐标  ,      由于高度增加也是对应的增加一个位置的坐标所以可以先相加
			var _x:Number = org_x + this._tileW/2 * (y + x);
				//y坐标亦相同
			var _y:Number = org_y + this._tileH/2 * (x - y);
			return new Point(_x,_y);
		}
		
			/**
			 * 统计位置  
			 * @return 
			 * 
			 */
		public function countSite(rect:Rectangle):Vector.<Point>{
			
			var result:Vector.<Point>=new Vector.<Point>();
			
			var Multiple:int=(rect.width>_tileW)&&(rect.height>_tileH)?2:10;
			var row:uint=Math.ceil(rect.width/_tileW*Multiple);
			var col:uint=Math.ceil(rect.height/_tileH*Multiple);
			
			row=row==0?1:row;
			col=col==0?1:col;
			
			
			
			var rowi:int=0;
			var coli:int=0;
			var point:Point;
			for(;rowi<row;rowi++){
				for(coli=0;coli<col;coli++){
					point=TouchGrid(rect.x+(rowi*_tileW/Multiple),rect.y+(coli*_tileH/Multiple));
					if(checkAdd(point,result)){
						result.push(point);
					}
				}
			}
			return result;
			
		}
		
		/**
		 * 检查重叠 
		 * 
		 */
		private function checkAdd(_cPoint:Point,list :Vector.<Point>):Boolean{
			var result:Boolean=true;
			var point:Point;
			if(_cPoint.x<0
				||_cPoint.y<0
				||_cPoint.x>=_xCount
				||_cPoint.y>=_yCount
			){
				result=false;
			}else{
				for each(point in list){
					if(point.x==_cPoint.x
						&&point.y==_cPoint.y
					){
						result=false;
						break;
					}
				}
			}
			

			return result;
		}
	}//end class
}